package com.seeyon.ctp.rest.resources;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.alibaba.fastjson.JSONArray;
import fy.config.FyConfig;
import fy.util.DBUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
//import com.seeyon.apps.zhongh.controller.HttpUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.principal.PrincipalManager;
import com.seeyon.ctp.services.security.ServiceManager;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.annotation.RestInterfaceAnnotation;


@Path("corp")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CorpDeptPost extends BaseResource{
    private static Log log = LogFactory.getLog(SrmUnitCode.class);

    //http://localhost/seeyon/rest/org/getUnitCode 根据华辉登录名获取NC登录名
    @Path("getCorp")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    //@getUnitCode
    public String getCorp(Map data){

        try{
            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
            conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());
            String sql="select PK_CORP,UNITNAME from corp";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            JSONArray ja = new JSONArray();
            while(rs.next()){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id",rs.getString(1));
                map.put("name",rs.getString(2));
                ja.add(map);
            }

            return "{\"code\":\"0\",\"message\":"+JSONObject.toJSONString(ja)+"}";
        }catch(Exception e){
            log.info("内容出错："+e.getMessage()+","+JSONObject.toJSONString(e.getStackTrace()));
        }finally{

        }
        return "{\"code\":\"0\",\"message\":\"获取事业部失败！\"}";
    }

    @Path("getDept")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)

    public String getDept(Map data){
        String pk_corp = data.get("pk_corp").toString();
        try{
            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
            conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());
            String sql="select d.pk_deptdoc, d.deptname from bd_deptdoc d left join bd_corp c on d.pk_corp = c.pk_corp where d.dr = 0 and c.pk_corp = ? and d.pk_fathedept is null order by c.pk_corp asc";
            pst = conn.prepareStatement(sql);
            pst.setString(1,pk_corp);
            rs = pst.executeQuery();
            JSONArray ja = new JSONArray();
            //Map<String, Object> map = new HashMap<String, Object>();
            while(rs.next()){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id",rs.getString(1));
                map.put("name",rs.getString(2));
                ja.add(map);
            }
            return "{\"code\":\"0\",\"message\":"+JSONObject.toJSONString(ja)+"}";
        }catch(Exception e){
            log.info("内容出错："+e.getMessage()+","+JSONObject.toJSONString(e.getStackTrace()));
        }finally{

        }
        return "{\"code\":\"0\",\"message\":\"获取部门失败！\"}";
    }

    @Path("getChildDept")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)

    public String getChildDept(Map data){
        String pk_deptdoc = data.get("pk_deptdoc").toString();
        try{
            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
            conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());
            String sql="select d.pk_deptdoc, d.deptname from bd_deptdoc d where d.pk_fathedept = ? and d.dr = 0";
            pst = conn.prepareStatement(sql);
            pst.setString(1,pk_deptdoc);
            rs = pst.executeQuery();
            JSONArray ja = new JSONArray();
            //Map<String, Object> map = new HashMap<String, Object>();
            while(rs.next()){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id",rs.getString(1));
                map.put("name",rs.getString(2));
                ja.add(map);
            }
            return "{\"code\":\"0\",\"message\":"+JSONObject.toJSONString(ja)+"}";
        }catch(Exception e){
            log.info("内容出错："+e.getMessage()+","+JSONObject.toJSONString(e.getStackTrace()));
        }finally{

        }
        return "{\"code\":\"0\",\"message\":\"获取子部门失败！\"}";
    }

    @Path("getPost")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)

    public String getPost(Map data){
        String pk_deptdoc = data.get("pk_deptdoc").toString();
        try{
            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
            conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());
            String sql="select j.jobcode, j.jobname, d.docname from om_job j left join bd_defdoc d on j.jobrank = d.pk_defdoc where j.pk_deptdoc = ? and j.dr = 0 order by j.jobcode";
            pst = conn.prepareStatement(sql);
            pst.setString(1,pk_deptdoc);
            rs = pst.executeQuery();
            JSONArray ja = new JSONArray();
            //Map<String, Object> map = new HashMap<String, Object>();
            while(rs.next()){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id",rs.getString(1));
                map.put("name",rs.getString(2));
                ja.add(map);
            }
            return "{\"code\":\"0\",\"message\":"+JSONObject.toJSONString(ja)+"}";
        }catch(Exception e){
            log.info("内容出错："+e.getMessage()+","+JSONObject.toJSONString(e.getStackTrace()));
        }finally{

        }
        return "{\"code\":\"0\",\"message\":\"获取岗位失败！\"}";
    }


}